<?php

namespace App\Listeners;

use App\Enum\LogsEnum;
use App\Enum\OrderDyplsEnum;
use App\Enum\OrderEnum;
use App\Enum\SmsScheduleEnum;
use App\Events\OrderCanceled;
use App\Models\DriverModel;
use App\Models\LogsModel;
use App\Models\OrderDyplsModel;
use App\Models\SmsScheduleModel;
use App\Models\TransportModel;
use App\Models\VillagerUserModel;
use Illuminate\Contracts\Queue\ShouldQueue;

class OrderCanceledNotification implements ShouldQueue
{
    public $queue= 'order-cancel';
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderCanceled  $event
     * @return void
     */
    public function handle(OrderCanceled $event)
    {
        $fromBackend = false;$adminId = 0;$adminName='';

        $userId = $event->orderInfo->user_id;
        $driverId = $event->orderInfo->driver_id;
        $transportId = $event->orderInfo->transport_id;
        $orderNo = $event->orderInfo->order_no;

        // 如果是后台请求取消订单
        if( isset($event->adminInfo['admin_id']) && !empty($event->adminInfo['admin_id'])){
            $adminId    = $event->adminInfo['admin_id'];
            $adminName  = $event->adminInfo['admin_name'];
            $fromBackend = true;
        }
        // 通知解绑 20230921 by LC
        OrderDyplsModel::query()->where(['order_id'=>$event->orderInfo->id])->update(['status'=>OrderDyplsEnum::STATUS_UNBIND_TODO]);

        // 添加订单日志 20230901 by LC
        LogsModel::query()->create([
            'type'      => LogsEnum::LOGS_TYPE_ORDER,
            'ref_id'    => $event->orderInfo->id,
            'ref_no'    => $event->orderInfo->order_no,
            'user_id'   => $fromBackend?$adminId:$event->orderInfo->user_id,
            'user_type' => $fromBackend?LogsEnum::LOGS_USER_TYPE_BACKEND:LogsEnum::LOGS_USER_TYPE_NORMAL,
            'username'  => $fromBackend?$adminName:LogsEnum::LOGS_USER_NAME,
            'content'   => "取消订单。".OrderEnum::STATUS_NAME[$event->orderOldStatus].'->取消',
        ]);

        // 司机信息或许有
        if( !empty($driverId) ) {
            $phone = DriverModel::query()->where('id', $driverId)->value('phone');
            if ( !empty($phone)) {
                // 写入司机的短信
                $params = ['name' => SmsScheduleEnum::SMS_DEFAULT_USERNAME, 'code' => $orderNo];
                $data = [
                    'source' => SmsScheduleEnum::SMS_SOURCE_API,
                    'mobile' => $phone,
                    'template_id' => SmsScheduleEnum::TEMPLATE_ID_ORDER_CANCEL_DRIVER,
                    'params' => json_encode($params),
                    'status' => SmsScheduleEnum::SMS_STATUS_UNSEND,
                ];
                SmsScheduleModel::query()->insert($data);
            }
        }
        // 承运商信息或许有
        if( !empty($transportId) ) {
            $phone = TransportModel::query()->where('id', $transportId)->value('phone');
            if ( !empty($phone)) {
                // 写入司机的短信
                $params = ['name' => SmsScheduleEnum::SMS_DEFAULT_USERNAME, 'code' => $orderNo];
                $data = [
                    'source' => SmsScheduleEnum::SMS_SOURCE_API,
                    'mobile' => $phone,
                    'template_id' => SmsScheduleEnum::TEMPLATE_ID_ORDER_CANCEL_TRANSPORT,
                    'params' => json_encode($params),
                    'status' => SmsScheduleEnum::SMS_STATUS_UNSEND,
                ];
                SmsScheduleModel::query()->insert($data);
            }
        }


        // 写入用户的短信
        $phone = VillagerUserModel::query()->where('id',$userId )->value('phone');
        if( !empty($phone)) {
            $params = ['name' => SmsScheduleEnum::SMS_DEFAULT_USERNAME, 'code' => $orderNo];
            $data = [
                'source' => SmsScheduleEnum::SMS_SOURCE_API,
                'mobile' => $phone,
                'template_id' => SmsScheduleEnum::TEMPLATE_ID_ORDER_CANCEL_USER,
                'params' => json_encode($params),
                'status' => SmsScheduleEnum::SMS_STATUS_UNSEND,
            ];
            SmsScheduleModel::query()->insert($data);
        }
    }
}
